<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Block extends CIC_A_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->init();
	}
	
	private function init()
	{
		$this->load->helper('ckeditor');
	}
	
	function index()
	{
		$this->admin_priv(__METHOD__);
		$data	= $this->get_block();
		$this->load->view('admin/block/index',$data);
	}
	
	function get_block()
	{
		$where	= '';
		$and 	= '';
		$url = site_url('admin/block/index?');
		$category_id = $this->input->get('category_id');
		$key_words	 = $this->input->get('key_words');
		if(! empty($category_id))
		{
			$where .= 'category_id = '.$category_id;
			$and	= ' and ';
			$url   .= 'category_id='.$category_id;
		}
		if(! empty($key_words))
		{
			$where .= $and."block_name like '%$key_words%'";
			$url   .= '&key_words='.$key_words;
		}
		$data['page']	 = $this->page($url, $this->cic_model->get_total_rows('block',$where), $this->per_page,TRUE);
		$data['block'] = $this->cic_model->get_all('block',$where,'',$this->per_page,$this->input->get('per_page'));
		return $data;
	}
	
	function content_add()
	{
		$data = array(
				'ckeditor_config'	=> $this->ckeditor_config('ckeditor'),
				'time_now'			=> date('Y-m-d',strtotime('10 day')),
		);
		$this->load->view('admin/block/content_add',$data);
	}
	
	function content_edit()
	{
		$data = array(
				'ckeditor_config'	=> $this->ckeditor_config('ckeditor'),
				'block'			=> $this->cic_model->get_one('block',array('block_id'=>$this->uri->segment(4))),
		);
		$this->load->view('admin/block/content_edit',$data);
	}
	function add_content()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('block', 'admin/block/content_add');
		$data = array(
				'block_name'		=> $this->input->post('block_name'),
				'block_content'		=> $this->input->post('block_content',FALSE),	
		);
		if($this->cic_model->insert('block',$data))
		{
			$this->message->msg('添加成功！',site_url('admin/block/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/block/content_add'));
		}
	}
	
	function edit_content()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('edit_block', 'admin/block/content_edit/'.$this->uri->segment(4));
		$data = array(
				'block_name'			=> $this->input->post('block_name'),
				'block_content'			=> $this->input->post('block_content',FALSE),
		);
		if($this->cic_model->update('block',$data,array('block_id'=>$this->uri->segment(4))))
		{
			$this->message->msg('修改成功！',site_url('admin/block/index'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/block/content_edit/'.$this->uri->segment(4)));
		}
	}
	

	
	function delete_block()
	{
		$this->admin_priv(__METHOD__);
		if($this->cic_model->delete('block',array('block_id'=>$this->input->post('block_id'))))
		{
			echo 1;
		}else{
			echo 0;
		}
	}
}